<?php
/**
 * alltosun.com 推送消息列表 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2011 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 雷健雄 (leijx@alltosun.com) $
 * $Date: 2015-2-26 上午11:05:21 $
 * $Id: index.php 211119 2015-02-28 07:12:27Z leijx $
 */


class Action
{
    private $per_page = 30;

    public function __call($action = '', $params = array())
    {
        $search_filter = array();
        $filter  = array();
        $order   = ' ORDER BY `id` DESC ';
        $page_no = Request::getParam('page_no', 1);
        $status  = Request::getParam('status', 1);

        $search_filter = Request::getParam('search_filter', array());
        $default_value  = array(
                'nick_name'   => '',
                'id'          => '',
                'add_time >=' => '',
                'add_time <=' => ''
        );
        // $search_filter  = set_search_filter_default_value($search_filter, $default_value);
        $filter = array('1'=>1);
        $filter['status'] = $status;

        /*
        $user_ids = _model('push')->getFields('user_id', array(1=>1));
        $user_ids = array_unique($user_ids);
        $user_list = _model('user')->getList($user_ids);

        // 得到得到昵称和系统消息id的一维数组
        $user_nick_names = array_to_option($user_list, 'nick_name');
        //系统消息昵称搜索
        if (isset($search_filter['nick_name']) && $search_filter['nick_name']) {
            foreach ($user_nick_names as $k => $nick_name) {
                if (strstr($nick_name, AnFilter::filter_string(trim($search_filter['nick_name'])))) {
                    $filter['user_id'][] = $k;
                }
            }
        }

        // 系统消息id搜索
        if (isset($search_filter['user_id']) && $search_filter['user_id']) {
            $filter_user_ids = explode(',', trim($search_filter['user_id'], ','));
            if ($filter['user_id']) {
                $filter['user_id'] = array_merge($filter_user_ids, $filter['user_id']);
            } else {
                $filter['user_id'] = $filter_user_ids;
            }
        }
        */

        if (isset($search_filter['receiver_type']) && $search_filter['receiver_type']) {
            $filter['receiver_type'] = $search_filter['receiver_type'];
        }

        if (isset($search_filter['start_add_time']) && $search_filter['start_add_time']) {
            $filter['add_time >= '] = $search_filter['start_add_time'].' 00:00:00';
        }
        if (isset($search_filter['end_add_time']) && $search_filter['end_add_time']) {
            $filter['add_time <= '] = $search_filter['end_add_time'].' 23:59:59';
        }

        $total = _model('push')->getTotal($filter);
        if ($total) {
            $list = get_data_list('push', $filter, $order, $page_no, $this->per_page);
            $push_list = array();
            $receiver_type = push_config::$receiver_type;

            foreach ($list as $k => $info) {
                $info['receiver_type'] = $receiver_type[$info['receiver_type']];
                // 分类名称
                // $info['res_name'] =
                $push_list[] = $info;
            }
            Response::assign('push_list', $push_list);
        }

        Response::assign('filter', $filter);
        Response::assign('search_filter', $search_filter);
        Response::assign('status', $status);
        Response::display('admin/push_list.html');
    }

    /**
     * 推送广播消息
     */
    public function add_all()
    {
        Response::display('admin/push_add.html');
    }

    /**
     * 推送指定用户消息
     */
    public function add_reg()
    {
        $receiver_type = 4;

        Response::assign('receiver_type', $receiver_type);
        Response::display('admin/push_add.html');
    }

    /**
     * 推送指定用户组
     */
    public function add_tag()
    {
        $receiver_type = 2;

        Response::assign('receiver_type', $receiver_type);
        Response::display('admin/push_add.html');
    }

    /**
     * 保存推送记录，并执行推送
     * @return unknown|multitype:string
     */
    public function save()
    {
        $info = Request::getParam('info', array());
        $result = _widget('push')->save_push_data($info);
        if (is_string($result)) {
            return $result;
        }
        return array('操作成功', 'success', AnUrl('push/admin'));
    }

    /**
     * 删除操作
     * @return string
     */
    public function delete()
    {
        $id = Request::getParam('id', '');
        $ids = explode(',', trim($id, ','));

        $count = count($ids);
        if ($count < 1) {
            return '操作id不准确';
        }

        $push_list = _model('push')->getList($ids);
        foreach ($push_list as $push_info) {
            if (!$push_info || !in_array($push_info['id'], $ids)) {
                continue;
            }
            if (1 != $push_info['status']) {
                return '该系统消息已经删除了';
            }
            $control = _model('push')->update($push_info['id'], array('status'=>0));
            if ($control) {
                // _widget('log')->record('push', $id, '修改');
            }
        }
        return 'ok';
    }

    public function recover()
    {
        $id = Request::getParam('id', '');
        $push_info = _uri('push', $id);

        if (!$push_info || !$id) {
            return '操作id不准确';
        }

        if (0 != $push_info['status']) {
            return '该推送消息已经还原';
        }
        $control = _model('push')->update($push_info['id'], array('status'=>1));
        if ($control) {
            // _widget('log')->record('push', $push_info['id'], '修改');
        }
        return 'ok';
    }

    public function thorough_delete()
    {
        $id = Request::getParam('id', 0);
        $push_info = _uri('push', $id);

        if (!$id) {
            return '请传递要操作的id';
        }
        if (!$push_info) {
            return '要操作的信息不存在';
        }

        $control = _model('push')->delete($id);
        if ($control) {
            // _widget('log')->record('push', $id, '删除');
            return 'ok';
        }
        return '操作失败';
    }

    // 测试推送
    public function test_push()
    {
        $msg = array(
                'content' => '推送测试 date: '.date('H:i:s'),
                'res_name' => 'goods',
                'res_id' => 10001
        );
        // $user_ids = array(25);

        $result = _widget('push')->send($msg);
        p($result);
    }
}
?>